package ytdp.careagle.com.ytdp.mvp.presenter;

import android.content.Context;

import java.util.HashMap;
import java.util.List;

import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.entity.BrandRelationship;
import ytdp.careagle.com.ytdp.entity.ModelRelationship;
import ytdp.careagle.com.ytdp.entity.SeriesRelationship;
import ytdp.careagle.com.ytdp.mvp.mode.CarEvaluateModel;
import ytdp.careagle.com.ytdp.mvp.view.SelectCartypeView;
import ytdp.careagle.com.ytdp.net.MyObserver;
import ytdp.careagle.com.ytdp.net.NetWorks;
import ytdp.careagle.com.ytdp.net.Results;


/**
 * Created by admin on 2017/9/20.
 */

public class SelectCarTypePresenter {
    private CarEvaluateModel model;
    private SelectCartypeView view;

    public SelectCarTypePresenter(SelectCartypeView view) {
        this.view = view;
        model = new CarEvaluateModel();
    }

    public void getList1Data(Context context) {
        view.showProgress("请稍等");
        model.getBrandRelationship(context, G.getHeader(context), new MyObserver<Results<BrandRelationship>>() {
            @Override
            public void onNext(Results<BrandRelationship> brandRelationship) {
                if (G.REQUEST_OK == brandRelationship.getCode()) {
                    List<BrandRelationship> dataBeen = brandRelationship.getData();
                    view.setList1Data(dataBeen);
                } else {
                    view.showMessage(brandRelationship.getMessage().toString());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.hideProgress();
                view.showMessage("获取数据失败");
            }
        });
    }

    public void getList2Data(Context context, String brandCode) {
        view.showProgress("请稍等");
        model.getList2Data(context, brandCode, new MyObserver<Results<SeriesRelationship>>() {
            @Override
            public void onNext(Results<SeriesRelationship> brandRelationship) {
                if (brandRelationship.getCode() == G.REQUEST_OK) {
                    List<SeriesRelationship> data = brandRelationship.getData();
                    view.setList2Data(data);
                } else {
                    if (brandRelationship.getCode() == G.SERVICE_ERROR) {
                        view.hideProgress();
                        view.showMessage("服务器发生错误");
                        return;
                    }
                    view.showMessage(brandRelationship.getMessage());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("数据获取失败");
                view.hideProgress();
            }
        });
    }

    public void getList3Data(Context context, String code) {
        view.showProgress("请稍等");
        HashMap<String, String> map = new HashMap<>();
        map.put("seriesCode", code);
        model.getModelRelationship(context, G.getHeader(context), map, new MyObserver<Results<ModelRelationship>>() {
            @Override
            public void onNext(Results<ModelRelationship> modelRelationship) {
                if (modelRelationship.getCode() == 200) {
                    List<ModelRelationship> data =
                            modelRelationship.getData();
                    view.setList3Data(data);


                } else {
                    view.showMessage(modelRelationship.getMessage().toString());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                view.showMessage("数据获取失败");
                view.hideProgress();
            }
        });
    }
}
